home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 242 / Issue 242 - April 2008 - DPCS0408DVD.ISO / Software Money Savers / VirtualDub / Source / VirtualDub-1.7.7-src.7z / src / system / source / list.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-11-05  |  2.5 KB  |  98 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. ///////////////////////////////////////////////////////////////////////////
  27. //
  28. //    For those of you who say this looks familiar... it should.  This is
  29. //    the same linked-list style that the Amiga Exec uses, with dummy head
  30. //    and tail nodes.  It's really a very convienent way to implement
  31. //    doubly-linked lists.
  32. //
  33.  
  34. #include "stdafx.h"
  35. #include <algorithm>
  36. #include <vd2/system/list.h>
  37.  
  38. List::List() {
  39.     Init();
  40. }
  41.  
  42. void List::Init() {
  43.     head.next = tail.prev = 0;
  44.     head.prev = &tail;
  45.     tail.next = &head;
  46. }
  47.  
  48. ListNode *List::RemoveHead() {
  49.     if (head.prev->prev) {
  50.         ListNode *t = head.prev;
  51.  
  52.         head.prev->Remove();
  53.         return t;
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
  59. ListNode *List::RemoveTail() {
  60.     if (tail.next->next) {
  61.         ListNode *t = tail.next;
  62.  
  63.         tail.next->Remove();
  64.         return t;
  65.     }
  66.  
  67.     return 0;
  68. }
  69.  
  70. void List::Take(List &from) {
  71.     if (from.IsEmpty())
  72.         return;
  73.  
  74.     head.prev = from.head.prev;
  75.     tail.next = from.tail.next;
  76.     head.prev->next = &head;
  77.     tail.next->prev = &tail;
  78.  
  79.     from.Init();
  80. }
  81.  
  82. void List::Swap(List &dst) {
  83.     if (IsEmpty())
  84.         Take(dst);
  85.     else if (dst.IsEmpty())
  86.         dst.Take(*this);
  87.     else {
  88.         std::swap(head.prev, dst.head.prev);
  89.         std::swap(tail.next, dst.tail.next);
  90.  
  91.         head.prev->next = &head;
  92.         tail.next->prev = &tail;
  93.  
  94.         dst.head.prev->next = &dst.head;
  95.         dst.tail.next->prev = &dst.tail;
  96.     }
  97. }
  98.